package com.y.comtoo.file;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.y.comtoo.utils.StrUtil;

public class FileFind {

	//	public static final String SourceDir = "C:\\Users\\Administrator\\Desktop\\20241012room";
	public static final String SourceDir = "D:\\apache-tomcat-8.5.100\\webapps\\oss";
//	public static final String SourceDir = "D:\\apache-tomcat-8.5.100\\webapps\\oss\\WEB-INF\\views";

	public static void main(String[] args) throws IOException {
		String keyword = "changeSelTreeNode";
    	File dir = new File(SourceDir);
    	Map<String, List<ExistRecord>> filePaths = SearchKeyword(dir, keyword, new HashMap<>());

		/* 要写入新文件的每行数据，如果查找结果过多，可以考虑将查找写过写入文件
		LinkedList<String> newLins = new LinkedList<String>();
		Files.write(Paths.get(newFullName), newLins, StandardOpenOption.WRITE);
		 */
    	for(String relPath : filePaths.keySet()) {
    		System.out.println("\n\n文件名：" + relPath);
    		List<ExistRecord> erList = filePaths.get(relPath);
    		for(ExistRecord er : erList) {
    			System.out.println("    前一行：" + er.preStr());
    			System.out.println("  行号：" + er.validNum() + ", 内容：" + er.validStr());
    			System.out.println("    后一行：" + er.nextStr());
    			System.out.println();
    		}
    	}
	}
	
	public static Map<String, List<ExistRecord>> SearchKeyword(File dir,final String keyword, Map<String, List<ExistRecord>> filePaths) throws IOException {
		/* <script>alert("static roomView.jsp ");</script> */
    	File[] flist = dir.listFiles();
    	if(null == flist) {
    		return filePaths;
    	}
    	String preStr = null;
    	String validPreStr = null;
    	String validStr = null;
    	String currStr = null;
    	int validNum = -1;
    	
    	for(File f : flist) {
    		if(f.isDirectory()) {
    			SearchKeyword(f, keyword, filePaths);
    		}else {
    			String oldPath = f.getAbsolutePath();
    			try {
            		String relPath = StrUtil.replaceAll(oldPath.replace(SourceDir,""),"\\", "/");
            		List<String> lines = Files.readAllLines(Paths.get(f.getPath()));
            		Iterator<String> s = lines.iterator();
            		List<ExistRecord> erList = new ArrayList<>();
            		int lineNum = 0;
            		while(s.hasNext()) {
        				preStr = currStr;
            			lineNum++;
            			currStr = s.next();
            			if(StrUtil.NotBlankIgnoreNullStr(validStr)) {
            				erList.add(new ExistRecord(validPreStr, validStr, validNum, currStr));
            				validPreStr = null;
            		    	validStr = null;
            			}
            			if(currStr.contains(keyword)) {
            				validPreStr = preStr;
            				validStr = currStr;
            				validNum = lineNum;
            			}
            		}
            		if(StrUtil.NotBlankIgnoreNullStr(validStr)) {
            			erList.add(new ExistRecord(validPreStr, validStr, validNum, null));
        			}
            		if(erList.size()>0) {
            			filePaths.put(relPath, erList);
            		}
    			}catch(Exception ex) {
    				ex.printStackTrace();
    			}
    		}
    	}
    	return filePaths;
	}
	
	/** 数据存在记录 */
	record ExistRecord(String preStr,String validStr, int validNum, String nextStr){
		
	}

}
